import axios from 'axios';
import yz from 'm/comm/yz.js';
import ajax from 'm/ajax/ajaxPost.js';
import Vue from 'vue'
import math from 'mathjs';
import $ from 'jquery';
import dCustomerOrder from '../../../models/dict/customerOrder';
import moment from 'moment'
import clType from '../../../models/dict/clothesSizeType'
import task from "../../../models/dict/task.js"  //任务字典模块
export default {
    data() {
        return {


            currObj: {},
            loading: true,
            countList: [],
            num: 0,
            amount100: 0,
            amount200_1: 0,
            amount200_2: 0,
            amount: 0,
            memberTell: ""



        }
    },
    methods: {

        getCustomerOrder: function () {
            this.loading = true

            ajax.post({
                class: "customerOrder",
                para: "getCustomerOrder",
                j: {
                    _id: this.customerOrderId
                },
                fun: (res) => {
                    if (res.data.err) {
                        this.$message({
                            showClose: true,
                            message: res.data.err,
                            type: 'error'

                        });
                    }
                    else {
              

                        this.currObj = dCustomerOrder.fmtCustomerOrder(res.data.info)
                        this.currObj[this.flowClassId] = dCustomerOrder.fmtPrint(this.currObj[this.flowClassId])

                        this.$message({
                            showClose: true,
                            message: "已获取打印表单!",
                            type: 'success'

                        });

                        this.getMember(this.currObj.salesMembers.memberId)



                        this.count()
                        this.loading = false;
                    }


                }
            })

        },
        //打印按钮
        printPage: function (params) {

            ajax.post({
                class: "customerOrder",
                para: "setPrint",
                j: {
                    _id: this.currObj._id,
                    key: "gongyidan",
                    val: this.currObj[this.flowClassId].print.gongyidan,
                    flowClassId: this.flowClassId
                },
                fun: (res) => {
                    if (res.data.err) {
                        this.$message({
                            showClose: true,
                            message: res.data.err,
                            type: 'error'

                        });
                    }
                    else {
                        //      this.currObj = dCustomerOrder.fmtCustomerOrder(res.data.info)





                        var prnhtml = $("#print").clone(true)

                        $(window.document.getElementById("ifm").contentWindow.document.body).html(prnhtml)
                        this.$message({
                            showClose: true,
                            message: "打印备注已经保存, 打印正在启动",
                            type: 'info'

                        });
                        setTimeout(function () {
                            window.document.getElementById("ifm").contentWindow.print()
                        }, 1000);

                    }


                }
            })






        },
        fmtDate(dateTime) {
            if (!dateTime) {
                dateTime = new Date()
            }
            return moment(dateTime).format("YYYY-MM-DD HH:mm")
        },
        count100: function () {
            var amount = 0;

            if (this.currObj[this.flowClassId].t100) {
                this.currObj[this.flowClassId].t100.forEach(function (item) {
                    amount = math.chain(item.amount).add(amount).done()

                }, this);

                this.amount100 = amount
            }
        },

        countNum: function () {

            var num = 0;
            this.currObj[this.flowClassId].data.forEach(function (item) {

                item.sizeArray.forEach(function (sizeItem) {
                    num = math.chain(sizeItem.num).add(num).done()

                }, this);

            }, this);

            this.num = num
        },
        count() {

            this.count100()
            this.countNum();
            this.countList.push({
                name: "面辅料",
                num: this.num,
                price: this.amount100
            })
            this.countList.push({
                name: "加工费/其他费用",
                num: this.num,
                price: this.amount200_2
            })
            if (this.currObj[this.flowClassId].t200) {
                this.currObj[this.flowClassId].t200.detail.forEach(function (item) {
                    item.name = item.wageClass.name
                    this.countList.push(item)
                }, this);
            }


            var amount = 0;
            this.countList.forEach(function (item) {



                item.amount = math.chain(item.num).multiply(item.price).round(2).done()
                amount = math.chain(item.amount).add(amount).done()

            }, this);

            this.amount = amount

        },
        getSizeName(sizeId) {

            return clType.getSizeName(sizeId)

        },
        getTechnologyName(technologyId) {
            return task.getTechnologyName(technologyId)
        },
        heji(array) {
            var amount = 0;
            array.forEach(function (item) {
                amount = math.chain(amount).add(item.num).done()

            }, this);

            return amount
        },
        getMember: function (memberId) {
            this.memberTell = ""
            if (!memberId) {
                return
            }
            ajax.post({
                class: "member",
                para: "getMember",
                j: {
                    _id: memberId
                },
                fun: (res) => {

                    this.memberTell = res.data.info.tell

                }
            })

        }


    },
    mounted() {
        this.getCustomerOrder()

    },
    props: {
        customerOrderId: {},
        flowClassId: {}

    },
    components: {

    }
}
